<template>
  <div class="app-container">
    <!--对象搜索树型列表-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="执行状态" prop="status">
        <y-select v-model="queryParams.status" placeholder="请选择执行状态" :options="tms_job_status" />
      </el-form-item>
      <el-form-item label="执行时间" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--对象操作-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Back" @click="handleBack">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['tms:job:delete']">删除</el-button>
      </el-col>
      <y-right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" />
    </el-row>

    <!--对象列表-->
    <el-table ref="tableRef" v-loading="loading" row-key="id" :data="jobExecutionList"
      @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" reserve-selection />
      <el-table-column label="序号" width="80" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="执行编号" align="center" prop="id" width="300" v-if="columns[0].visible" sortable
        :show-overflow-tooltip="true">
        <template #default="scope">
          <router-link :to="'/tms/job-info/index?job=' +
      scope.row.job +
      '&job_execution=' +
      scope.row.id
      " class="link-type">
            <span>{{ scope.row.id }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="执行名称" align="center" prop="name" v-if="columns[1].visible" sortable
        :show-overflow-tooltip="true">
        <template #default="scope">
          <router-link :to="'/tms/job-info/index?job=' +
      scope.row.job +
      '&job_execution=' +
      scope.row.id
      " class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="执行状态" align="center" prop="status" width="110" v-if="columns[2].visible" sortable>
        <template #default="scope">
          <y-dict-tag :options="tms_job_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="run_time" width="180" v-if="columns[3].visible" sortable>
        <template #default="scope">
          <span>{{ parseTime(scope.row.run_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="finished" width="180" v-if="columns[4].visible" sortable>
        <template #default="scope">
          <span>{{ parseTime(scope.row.finished) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行耗时(秒)" align="center" width="150" v-if="columns[5].visible" sortable>
        <template #default="scope">
          <span>{{
      (
        (new Date(scope.row.finished ? scope.row.finished : Date.now()) -
          new Date(
            scope.row.run_time ? scope.row.run_time : Date.now()
          )) /
        1000
      ).toFixed() + " 秒"
    }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="80">
        <template #default="scope">
          <el-tooltip content="统计" placement="top">
            <el-button link type="primary" icon="PieChart" @click="handleStatistical(scope.row)"
              v-hasPermi="['tms:job:log']">
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['tms:job:delete']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!--列表分页-->
    <y-pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.limit"
      @pagination="getList" />
    <!-- 添加或修改对话框 -->
    <statistic title="统计信息" :id="id" v-model="openStatistic" @close="openStatistic = false">
    </statistic>
  </div>
</template>

<script setup name="JobExecution">
import Statistic from "./components/Statistic";
import { listExecutionJob, delExecutionJob } from "@/api/tms/job/execution";

const route = useRoute();
const { proxy } = getCurrentInstance();
const { tms_job_status } = proxy.useDict("tms_job_status");

const id = ref(null);
const ids = ref([]);
const total = ref(0);
const loading = ref(true);
const showSearch = ref(false);
const single = ref(true);
const multiple = ref(true);
const dateRange = ref([]);
const openStatistic = ref(false);
const jobExecutionList = ref([]);

const columns = ref([
  { key: 0, label: "执行编号", visible: true },
  { key: 1, label: "执行名称", visible: true },
  { key: 2, label: "执行状态", visible: true },
  { key: 3, label: "开始时间", visible: true },
  { key: 4, label: "结束时间", visible: true },
  { key: 5, label: "执行耗时", visible: true },
]);

const data = reactive({
  form: {},
  queryParams: {
    job: route.query.job,
    status: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams, form } = toRefs(data);

async function getList() {
  loading.value = true;
  const response = await listExecutionJob(proxy.addDateRange(queryParams.value, dateRange.value));
  jobExecutionList.value = response.data;
  total.value = response.total;
  loading.value = false;
}

/** 返回操作 */
function handleBack() {
  proxy.$router.back();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 统计信息按钮操作 */
function handleStatistical(row) {
  id.value = row.id;
  openStatistic.value = true;
  form.value = row;
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    }
    else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delExecutionJob(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection()
    getList();
  } catch (error) {
    console.error(error);
  }
}

onMounted(() => {
  getList();
})

</script>